// service统一出口
import HYRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'
import { ElMessage } from "element-plus"
import { LocalCache } from "@/utils/localCache"

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      // 携带token的拦截
      if(config.url !== "/user/login") {
        const token = LocalCache.getItem("userInfo")?.token
        if(token){
          config.headers!.Authorization = `Bearer ${token}`
        } 
      }
      
      return config
    },
    requestInterceptorCatch: (err) => {
      return err
    },
    responseInterceptor: (res) => {
      console.log("响应:", res.data);
      if(res.data.code !== 200) {
        ElMessage.error(res.data.msg)
      }
      return res.data
    },
    responseInterceptorCatch: (err) => {
      return err
    }
  }
})

export default hyRequest
